package com.link.speechdemo.speech;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.link.speechdemo.MyApplication;
import com.link.speechdemo.Singleton;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechUnderstander;
import com.iflytek.cloud.SpeechUnderstanderListener;
import com.iflytek.cloud.UnderstanderResult;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by link on 2016/11/1.
 */

/**
 * 语音理解类
 */
public class UnderstanderSpeech {
    private Context context;
    private SpeechUnderstander understander;

    public UnderstanderSpeech() {
        context = MyApplication.getGlobalContext();
    }

    public void init() {
        setParam();
        //3.开始语义理解
        int str = understander.startUnderstanding(mUnderstanderListener);
        Log.d("understander", "执行到了" + str);
    }

    // XmlParser为结果解析类，请参照Demo
    private SpeechUnderstanderListener mUnderstanderListener = new SpeechUnderstanderListener() {
        @Override
        public void onVolumeChanged(int volume, byte[] data) {
            //volume音量值0~30，data音频数据
        }

        @Override
        public void onBeginOfSpeech() {
            //开始录音
            Log.d("开始录音", "start");
        }

        @Override
        public void onEndOfSpeech() {
            //结束录音
            Log.d("结束录音", "end");
        }

        @Override
        public void onResult(UnderstanderResult understanderResult) {
            String result = understanderResult.getResultString();
            try {
                JSONObject aaa = new JSONObject(result);
                String service = aaa.getString("service");
                Singleton.getJInstance().Judge(service, result);
            } catch (JSONException e) {
                e.printStackTrace();
                Singleton.getSInstance().init("很抱歉，我还不能理解这句话，不过相信我会尽快成长起来的。");
            }

        }

        @Override
        public void onError(SpeechError speechError) {
            //回话发生错误回调接口
            Log.e("Understander—Error", speechError.toString());
            understander.stopUnderstanding();
            myActivity.callBack();
        }

        @Override
        public void onEvent(int i, int i1, int i2, Bundle bundle) {
            //扩展用的接口
        }
    };

    private void setParam() {
        //1.创建文本语义理解对象
        understander = SpeechUnderstander.createUnderstander(context, null);
        //2.设置参数，语义场景配置请登录 http://osp.voicecloud.cn/
        understander.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        // 设置语音前端点:静音超时时间，即用户多长时间不说话则当做超时处理
        understander.setParameter(SpeechConstant.VAD_BOS, "8000");

        // 设置语音后端点:后端点静音检测时间，即用户停止说话多长时间内即认为不再输入， 自动停止录音
        understander.setParameter(SpeechConstant.VAD_EOS, "1000");

        // 设置标点符号，默认：1（有标点）
        understander.setParameter(SpeechConstant.ASR_PTT, "1");
    }

    /**
     * 使用回调方式获取activity对象
     */
    private iMyActivity myActivity;

    public interface iMyActivity {
        void callBack();
    }

    public void setCallBack(iMyActivity myActivity) {
        this.myActivity = myActivity;
    }

}
